package com.ruoyi.web.controller.system;

import java.io.IOException;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.exception.file.InvalidExtensionException;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.file.MimeTypeUtils;
import com.ruoyi.system.domain.TMember;
import com.ruoyi.system.domain.TMembershipConditions;
import com.ruoyi.system.domain.WorkDetailDTO;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.system.service.ITMemberService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.Works;
import com.ruoyi.system.service.IWorksService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * Controller
 * 
 * @author ruoyi
 * @date 2025-08-25
 */
@RestController
@RequestMapping("/system/works")
public class WorksController extends BaseController
{
    @Autowired
    private IWorksService worksService;
   /* @Autowired
    private ISysUserService userService;*/
    @Autowired
    private ITMemberService memberService;



    /*C端查看作品数据
    * */
    @GetMapping("/detail/{workId}")
    public AjaxResult getWorkDetail(@PathVariable Long workId){
        WorkDetailDTO workDetail= worksService.getWorkDetail(workId);
        return success(workDetail);
    }

    /*查询作品列表*/
    @PreAuthorize("@ss.hasPermi('system:works:list')")
    @GetMapping("/list")
    public TableDataInfo list(Works works)
    {
        startPage();
        List<Works> list = worksService.selectWorksLists(works);
        return getDataTable(list);
    }

    /*作品封面上传*/
    @PostMapping("/uploads")
    public AjaxResult avatar(@RequestParam("file") MultipartFile file) throws IOException, InvalidExtensionException {
        if (!file.isEmpty()){
            String avatar = FileUploadUtils.upload(RuoYiConfig.getAvatarPath(), file, MimeTypeUtils.IMAGE_EXTENSION, true);
            AjaxResult ajax = AjaxResult.success();
            ajax.put("imgUrl", avatar);
            return ajax;
        }
        return error("图片上传异常");
    }

    /*后端：作品增加：*/
    @PreAuthorize("@ss.hasPermi('system:works:add')")
    @Log(title = "新增作品", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Works works)
    {
        //查询会员列表，将会员表与作品表绑定
        //Long userId = works.getUserId();
        //Long memberId = userService.selectMemberIdByUserId(userId);
        return toAjax(worksService.insertWorks(works));
    }

    /*增加作品表时，回显会员名称和id*/
    @PreAuthorize("@ss.hasPermi('system:works:add')")
    @GetMapping("/select")
    public AjaxResult selectMemberInfo()
    {
        AjaxResult ajax = AjaxResult.success();
        //从会员表中根据用户id查询用户名称
        List<TMember> user = memberService.selectUserNameByUserId();
        //查询所有会员名称
        ajax.put(AjaxResult.DATA_TAG,user);
        return ajax;
    }

    /*修改作品*/
    @PreAuthorize("@ss.hasPermi('system:works:edit')")
    @Log(title = "修改作品", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Works works)
    {
        return toAjax(worksService.updateWorks(works));
    }

    /*删除作品*/
    @PreAuthorize("@ss.hasPermi('system:works:remove')")
    @Log(title = "删除作品", businessType = BusinessType.DELETE)
    @DeleteMapping("/{workIds}")
    public AjaxResult remove(@PathVariable Long[] workIds)
    {
        return toAjax(worksService.deleteWorksByWorkIds(workIds));
    }










    /**
     * 导出作品列表
     */
    @PreAuthorize("@ss.hasPermi('system:works:export')")
    @Log(title = "导出作品", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Works works)
    {
        List<Works> list = worksService.selectWorksList(works);
        ExcelUtil<Works> util = new ExcelUtil<Works>(Works.class);
        util.exportExcel(response, list, "导出作品数据");
    }

    /**
     * 获取作品详细信息，根据作品id查询用户信息
     */
    @PreAuthorize("@ss.hasPermi('system:works:query')")
    @GetMapping(value = "/{workId}")
    public AjaxResult getInfo(@PathVariable("workId") Long workId)
    {
        return success(worksService.selectWorksByWorkId(workId));
    }







}
